<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/page/common/commonConfig.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <base href="<%=basePath%>">
    <title>单位用户列表</title>
    <script src="<%=basePath%>component/layout.js"></script>
    <script src="<%=basePath%>component/toolbar.js"></script>
    <script src="<%=basePath%>component/grid.js"></script>
    <script type="text/javascript" src="<%=basePath%>dwrsystem/interface/userService.js"></script> 
	<script type="text/javascript" src="<%=basePath%>dwrsystem/engine.js"></script>
	<script src="<%=basePath%>dwrsystem/engine.js"></script>
	<script src="<%=basePath%>dwrsystem/interface/roleSystemService.js"></script>
	<script type="text/javascript" src="<%=basePath%>dwrsystem/engine.js"></script>
	<script type="text/javascript" src="<%=basePath%>dwrsystem/interface/UserRolesystemAuthorize.js"></script>
	<script type="text/javascript">
	var mygrid;
	var roleSystemId;
	var addUserWindow;
	var dhxLayout = createLayoutNormal('1C',false,true);

	var dhxGrid = {
		initDataTable : function () {
		    mygrid.setHeader("序号,登录姓名,登录账号,备注");
		    mygrid.setInitWidths("50,150,150,*");
		    mygrid.setColAlign("right,left,left,left");
		    mygrid.setColSorting("int,str,str,str");
		    mygrid.setColTypes("ro,ro,ro,ro");
		    mygrid.init();
		    setSkin(mygrid);
		    mygrid.loadXML('<%=basePath%>system-user/UserAction!userGridFromRoleSystem.action?roleSystemId=' + roleSystemId);
		},
		delOneFromRoleSystem : function(){
			var rowId = mygrid.getSelectedId();
			if(rowId){
				confirmInfo('从当前单位中，删除选中用户？',
					function(choose){
						if('ok' == choose){
							layoutProgressOn(dhxLayout);
							roleSystemService.deleteTUserRoleSystem({userMainId:rowId,roleSystemId:roleSystemId},function(data){
								var lastrowIndex = mygrid.getRowIndex(mygrid.getSelectedId())-1;
								mygrid.deleteSelectedRows();
								mygrid.selectRow(lastrowIndex);
								
								for(var i=(lastrowIndex+1) ;i<(mygrid.getRowsNum());i++){
									mygrid.cells(mygrid.getRowId(''+i),0).setValue(i+1);
								}
								parent.parent.notifyCreater('删除成功');
								layoutProgressOff(dhxLayout);
							});
						}else{
							layoutProgressOff(dhxLayout);
							return;
						}
					}
				);
			}else{
				parent.parent.notifyWarn('请选择删除项');
			}
		}
	};
	
	function queryUsers(){
		roleSystemService.selectExitsUsers({roleSystemId:roleSystemId},function(data){
			addUserWindow = openNewWindowURL('选择用户','<%=basePath%>page/system/chooseComponent/chooseUser.jsp?userIds='+data);
		});
	}
	
	function getChooseUsesrResult(ids,names,loginCodes,newaddIds,newaddNames,newaddLoginCodes,deletedIds){
		layoutProgressOn(dhxLayout);
		if((newaddIds != null && newaddIds != '') || (deletedIds != null && deletedIds != '')){
			UserRolesystemAuthorize.deleteAndinsertAllUsersInOneRoleSystem(newaddIds,deletedIds,roleSystemId,function(data){
				if(data){
					layoutProgressOff(dhxLayout);
					updateGrid(newaddIds,newaddNames,newaddLoginCodes,deletedIds);
					closeUserWindow();
				}else{
					parent.parent.notifyWarn('系统异常，请联系管理员');
					layoutProgressOff(dhxLayout);
				}
			});
		}else{
			parent.parent.notifyCreater('没有操作');
			layoutProgressOff(dhxLayout);
			closeUserWindow();
		}
	}
	
	function updateGrid(newaddIds,newaddNames,newaddLoginCodes,deletedIds){
		if(deletedIds != null && deletedIds != ''){
			var delIds = deletedIds.split(',');
			for(var j=0;j<delIds.length;j++){
				mygrid.deleteRow(delIds[j]);
				for(var i=0 ;i<(mygrid.getRowsNum());i++){
					mygrid.cells(mygrid.getRowId(''+i),0).setValue(i+1);
				}
			}
		}
		if(newaddIds != null && newaddIds != ''){
			var addIds = newaddIds.split(',');
			var names = newaddNames.split(',');
			var codes = newaddLoginCodes.split(',');
			for(var i=0;i<addIds.length;i++){
				mygrid.addRow(addIds[i], ''+(mygrid.getRowsNum()+1), ''+mygrid.getRowsNum());
				mygrid.cellById(addIds[i], 1).setValue(names[i]);
				mygrid.cellById(addIds[i], 2).setValue(codes[i]);
				if(i == (addIds.length-1)){
					mygrid.selectRowById(addIds[i]);
				}
			}
		}
		parent.parent.notifyCreater('操作成功');
	}
	
	function closeUserWindow(){
		if(addUserWindow){
			addUserWindow.close();
		}
	}

	function configLayout(){
		var a = dhxLayout.cells('a');
		a.hideHeader();
		a.setText('主页面');
		a.fixSize(true, true);
		mygrid = createGridFromLayoutCell(a);
		if(power.getPower() != '' && (power.getPowerInsert() || power.getPowerDelete()))
			createToolbarFromDHXLayoutCell(a,function(toolbar){
			    toolbar.addText("info", 1, "用户列表:");
			    toolbar.addSeparator("sep1", 2);
			    if(power.getPowerInsert()){
			    	toolbar.addButton("addExits", 4, "管理用户", "new.gif", "new_dis.gif");
			    }
			    if(power.getPowerDelete()){
			    	toolbar.addButton("delete", 5, "删除用户", "cut.gif", "cut_dis.gif");
			    }
			    toolbar.attachEvent("onClick", function(id) {
			    	if(id == 'addExits'){
			    		queryUsers();
			    	}
			    	if(id == 'delete'){
			    		dhxGrid.delOneFromRoleSystem();
			    	}
			    });
			});
	}
	
	$(function (){
		roleSystemId = '${param.roleSystemId}';
		configLayout();
		dhxGrid.initDataTable();
	});
	</script>
</head>

<body>
</body>

</html>
